package com.hjq.fcloud.widget

import android.content.Context
import android.util.AttributeSet
import android.view.View
import android.widget.Button
import android.widget.RelativeLayout
import com.blankj.utilcode.util.ClickUtils
import com.hjq.fcloud.R
import com.hjq.fcloud.ktx.ViewKtx.setVisibility


/**
 * 底部签约View
 */
class BottomSignView : RelativeLayout {

    private var passSignText:String = "通过"
    private var noPassSignText:String = "不通过"
    private var againPassSignText:String = "重新签约"

    constructor(context: Context?) : this(context, null)
    constructor(context: Context?, attrs: AttributeSet?) : this(context, attrs, 0)
    constructor(context: Context?, attrs: AttributeSet?, defStyleAttr: Int) : super(
        context,
        attrs,
        defStyleAttr
    ) {
        parseTypedArray(context,attrs)

        inflateView(attrs)
    }

    private val signView:View by lazy { findViewById(R.id.signView) }
    private val rejectSignBtn:View by lazy { findViewById(R.id.rejectSignBtn) }
    private val confirmSignBtn:View by lazy { findViewById(R.id.confirmSignBtn) }
    private val againSignBtn:View by lazy { findViewById(R.id.againSignBtn) }

    var mListener:BottomSignViewClickListener? = null

    private fun parseTypedArray(context: Context?, attrs: AttributeSet?) {
        val typedArray = context!!.obtainStyledAttributes(
            attrs,
            R.styleable.BottomSignView
        )
        if(typedArray.hasValue(R.styleable.BottomSignView_passSignText)){
            passSignText = typedArray.getString(
                R.styleable.BottomSignView_passSignText
            ).toString()
        }
        if(typedArray.hasValue(R.styleable.BottomSignView_noPassSignText)){
            noPassSignText = typedArray.getString(
                R.styleable.BottomSignView_noPassSignText
            ).toString()
        }
        if(typedArray.hasValue(R.styleable.BottomSignView_againPassSignText)){
            againPassSignText = typedArray.getString(
                R.styleable.BottomSignView_againPassSignText
            ).toString()
        }

        typedArray.recycle()
    }



    private fun inflateView(attrs: AttributeSet?) {
        inflate(context, R.layout.layout_bottom_sign_view, this)

        ClickUtils.applySingleDebouncing(rejectSignBtn,{
            mListener?.onNoPassSign()
        })

        ClickUtils.applySingleDebouncing(confirmSignBtn,{
            mListener?.onPassSign()
        })

        ClickUtils.applySingleDebouncing(againSignBtn,{
            mListener?.onAgainPassSign()
        })
    }

    /**
     * isAgainSign 是否重新签约 true 是否
     */
    fun setViewData(isAgainSign:Boolean,listener:BottomSignViewClickListener) {
        setVisibility(true)
        this.mListener = listener
        if (isAgainSign) {
            againSignBtn.setVisibility(true)
            signView.setVisibility(false)
        } else {
            againSignBtn.setVisibility(false)
            signView.setVisibility(true)
        }
    }

    /**
     * 不通过按钮
     */
    fun getRejectSignButton():View{
        return rejectSignBtn
    }
}

interface BottomSignViewClickListener{
    fun onAgainPassSign()//重新签约
    fun onPassSign()//通过
    fun onNoPassSign()//不通过
}
